cmake_minimum_required(VERSION 3.9.0)
project(build-deps NONE)
cmake_host_system_information(
  RESULT build_concurrency_factor
  QUERY NUMBER_OF_LOGICAL_CORES)
set(info_dir --infodir=@REDPANDA_DEPS_INSTALL_DIR@/share/info)
include(ExternalProject)

set(c_flags "@BASE_C_FLAGS@ @CMAKE_C_FLAGS_BUILD_TYPE@")
set(cxx_flags "@BASE_CXX_FLAGS@ @CMAKE_CXX_FLAGS_BUILD_TYPE@ -std=c++20")
set(ld_flags "@BASE_LD_FLAGS@")
set(default_depends "")

# There is a known issue with ExternalProject_Add's handling of list arguments.
# ref: https://public.kitware.com/Bug/view.php?id=16137
# Expected way to pass lists is by replacing ; separtor with a custom separator
# and the passing it via LIST_SEPARATOR param.
string (REPLACE ";" "|" CMAKE_C_COMPILER_LAUNCHER_STR "@CMAKE_C_COMPILER_LAUNCHER@")
string (REPLACE ";" "|" CMAKE_CXX_COMPILER_LAUNCHER_STR "@CMAKE_CXX_COMPILER_LAUNCHER@")

set(common_cmake_args
  -DCMAKE_CXX_STANDARD=@CMAKE_CXX_STANDARD@
  -DCMAKE_CXX_EXTENSIONS=@CMAKE_CXX_EXTENSIONS@
  -DCMAKE_C_COMPILER=@CMAKE_C_COMPILER@
  -DCMAKE_CXX_COMPILER=@CMAKE_CXX_COMPILER@
  -DCMAKE_C_COMPILER_LAUNCHER=${CMAKE_C_COMPILER_LAUNCHER_STR}
  -DCMAKE_CXX_COMPILER_LAUNCHER=${CMAKE_CXX_COMPILER_LAUNCHER_STR}
  "-DCMAKE_C_FLAGS=@BASE_C_FLAGS@"
  "-DCMAKE_C_FLAGS_@BUILD_TYPE@=@CMAKE_C_FLAGS_BUILD_TYPE@"
  "-DCMAKE_CXX_FLAGS=@BASE_CXX_FLAGS@ -std=c++20"
  "-DCMAKE_CXX_FLAGS_@BUILD_TYPE@=@CMAKE_CXX_FLAGS_BUILD_TYPE@"
  "-DCMAKE_EXE_LINKER_FLAGS=@CMAKE_EXE_LINKER_FLAGS@"
  "-DCMAKE_SHARED_LINKER_FLAGS=@CMAKE_SHARED_LINKER_FLAGS@"
  "-DCMAKE_MODULE_LINKER_FLAGS=@CMAKE_MODULE_LINKER_FLAGS@"
  -DCMAKE_BUILD_TYPE=@CMAKE_BUILD_TYPE@
  -DCMAKE_PREFIX_PATH=@REDPANDA_DEPS_INSTALL_DIR@
  -DCMAKE_INSTALL_PREFIX=@REDPANDA_DEPS_INSTALL_DIR@
  -DCMAKE_INSTALL_RPATH=@REDPANDA_DEPS_INSTALL_DIR@/lib
  -DCMAKE_SKIP_BUILD_RPATH=OFF
  -DCMAKE_BUILD_WITH_INSTALL_RPATH=ON
  -DCMAKE_POSITION_INDEPENDENT_CODE=@CMAKE_POSITION_INDEPENDENT_CODE@
  -DCMAKE_VERBOSE_MAKEFILE=@CMAKE_VERBOSE_MAKEFILE@
)
# pkgconfig
set(ENV{PKG_CONFIG_PATH}  @PKG_CONFIG_PATH@)
set(cmake_build_env)
if (@CMAKE_CXX_COMPILER_ID@ MATCHES "GNU")
  set(seastar_ld_flags "${ld_flags} -pthread")
  set(seastar_cxx_flags "-Wno-error$<SEMICOLON>-fcoroutines")
else()
  set(seastar_cxx_flags "-Wno-error")
endif()

if ("@CMAKE_CXX_COMPILER_ID@" MATCHES "Clang")
  set(AR_EXE llvm-ar)
else()
  set(AR_EXE ar)
endif()

# Find ar binary
execute_process(
  COMMAND
    @CMAKE_C_COMPILER@ @BASE_C_FLAGS@ -print-prog-name=${AR_EXE}
  OUTPUT_VARIABLE
    AR_PROGRAM
  OUTPUT_STRIP_TRAILING_WHITESPACE
)

# Get absolute path to binary
execute_process(
  COMMAND
    bash -c "type -p ${AR_PROGRAM}"
  OUTPUT_VARIABLE
    AR_PROGRAM
  OUTPUT_STRIP_TRAILING_WHITESPACE
)

# Cache AR
set(AR ${AR_PROGRAM} CACHE FILEPATH "AR")

# autotools
set(base_env
  CC=@CMAKE_C_COMPILER@
  CXX=@CMAKE_CXX_COMPILER@
  AR=${AR}
  PKG_CONFIG_PATH=@PKG_CONFIG_PATH@
  # Workaround for https://savannah.gnu.org/support/?110503
  # Starting with autoconf 2.70, it is expected that gtkdocize is installed.
  # Since we don't build thirdparty with docs, point gtkdocize elsewhere.
  GTKDOCIZE=/bin/true
)
set(configure_env
  ${base_env}
  CFLAGS=${c_flags}
  CXXFLAGS=${cxx_flags}
  LDFLAGS=${ld_flags}
)
# For a libtool project
set(build_env
  ${base_env}
  CFLAGS=${c_flags}
  CXXFLAGS=${cxx_flags}
  "LDFLAGS=${ld_flags}"
)

if ("@CMAKE_CXX_COMPILER_ID@" MATCHES "Clang")
    set(toolset clang)
    set(boost_user_config_jam "using clang : : @CMAKE_CXX_COMPILER@ $<SEMICOLON>")
elseif (@CMAKE_CXX_COMPILER_ID@ STREQUAL "GNU")
    set(toolset gcc)
    set(boost_user_config_jam "using gcc : : @CMAKE_CXX_COMPILER@ : $<SEMICOLON>")
endif()

# define make command in terms of cmake
set(make_command make ${build_env} -j${build_concurrency_factor} )
if ("@CMAKE_BUILD_TYPE@" MATCHES "Debug")
  set(BOOST_BUILD_VARIANT debug)
else ()
  set(BOOST_BUILD_VARIANT release)
endif()
ExternalProject_Add(boost
  # Boost outcome is only installed on boost 1.70.0+
  URL https://boostorg.jfrog.io/artifactory/main/release/1.75.0/source/boost_1_75_0.tar.gz
  URL_MD5 38813f6feb40387dfe90160debd71251
  INSTALL_DIR    @REDPANDA_DEPS_INSTALL_DIR@
  PATCH_COMMAND
    ./bootstrap.sh
    --prefix=@REDPANDA_DEPS_INSTALL_DIR@
    --with-libraries=atomic,chrono,date_time,filesystem,iostreams,program_options,regex,system,test,thread
    &&
    echo
    "${boost_user_config_jam}"
    > <SOURCE_DIR>/tools/build/src/user-config.jam
  CONFIGURE_COMMAND ""
  BUILD_COMMAND ""
  DEPENDS ${default_depends}
  INSTALL_COMMAND
    ${CMAKE_COMMAND} -E chdir <SOURCE_DIR>
    ./b2
    -j ${build_concurrency_factor}
    --toolset=${toolset}
    --layout=system
    --build-dir=<BINARY_DIR>
    install
    variant=${BOOST_BUILD_VARIANT}
    cflags=${c_flags}
    cxxflags=${cxx_flags}
    linkflags=${ld_flags}
    link=shared
    threading=multi
    hardcode-dll-paths=true
    dll-path=@REDPANDA_DEPS_INSTALL_DIR@/lib)

ExternalProject_Add(cryptopp
  URL https://github.com/weidai11/cryptopp/archive/refs/tags/CRYPTOPP_8_5_0.tar.gz
  URL_MD5 5968e6014dc6ae5199e3987fb39cf8d3
  PATCH_COMMAND
    COMMAND curl -s -o CMakeLists.txt https://raw.githubusercontent.com/noloader/cryptopp-cmake/CRYPTOPP_8_5_0/CMakeLists.txt
    COMMAND curl -s -o cryptopp-config.cmake https://raw.githubusercontent.com/noloader/cryptopp-cmake/CRYPTOPP_8_5_0/cryptopp-config.cmake
  INSTALL_DIR @REDPANDA_DEPS_INSTALL_DIR@
  CMAKE_COMMAND ${CMAKE_COMMAND} -E env ${cmake_build_env} ${CMAKE_COMMAND}
  DEPENDS ${default_depends}
  LIST_SEPARATOR |
  CMAKE_ARGS
     ${common_cmake_args}
    -DBUILD_TESTING=OFF)

ExternalProject_Add(fmt
  URL https://github.com/fmtlib/fmt/archive/refs/tags/8.1.1.tar.gz
  URL_MD5 fe7f1585844b14c647bd332ad5562832
  INSTALL_DIR @REDPANDA_DEPS_INSTALL_DIR@
  CMAKE_COMMAND ${CMAKE_COMMAND} -E env ${cmake_build_env} ${CMAKE_COMMAND}
  DEPENDS ${default_depends}
  LIST_SEPARATOR |
  CMAKE_ARGS
     ${common_cmake_args}
     -DFMT_DOC=OFF
     -DFMT_TEST=OFF)

ExternalProject_Add(seastar
  GIT_REPOSITORY https://github.com/redpanda-data/seastar.git
  GIT_TAG 30d3a28bde08d2228b4e560c173b89fdd94c3f05
  INSTALL_DIR    @REDPANDA_DEPS_INSTALL_DIR@
  CMAKE_COMMAND ${CMAKE_COMMAND} -E env ${cmake_build_env} ${CMAKE_COMMAND}
  LIST_SEPARATOR |
  CMAKE_ARGS
    ${common_cmake_args}
    -DBoost_USE_STATIC_LIBS=OFF
    -DBoost_NO_BOOST_CMAKE=ON
    -DBoost_NO_SYSTEM_PATHS=TRUE
    -DSeastar_INSTALL=ON
    -DSeastar_DPDK=OFF
    -DSeastar_APPS=OFF
    -DSeastar_DEMOS=OFF
    -DSeastar_DOCS=OFF
    -DSeastar_TESTING=OFF
    -DSeastar_CXX_FLAGS=${seastar_cxx_flags}
    "-DSeastar_LD_FLAGS=${seastar_ld_flags}"
    -DSeastar_API_LEVEL=6
    -DSeastar_CXX_DIALECT=c++20
    -DSeastar_UNUSED_RESULT_ERROR=ON
    -Dvalgrind_PC_INCLUDEDIR=<INSTALL_DIR>/include
  INSTALL_COMMAND ${CMAKE_COMMAND} --build <BINARY_DIR> --target install
  DEPENDS ${default_depends} boost cryptopp)

ExternalProject_Add(HdrHistogram
  URL https://github.com/HdrHistogram/HdrHistogram_c/archive/refs/tags/0.11.5.tar.gz
  URL_MD5 95f35abcaf7fae46e63a4dc4b6db81fd
  INSTALL_DIR    @REDPANDA_DEPS_INSTALL_DIR@
  CMAKE_COMMAND ${CMAKE_COMMAND} -E env ${cmake_build_env} ${CMAKE_COMMAND}
  LIST_SEPARATOR |
  CMAKE_ARGS
    ${common_cmake_args}
    -DHDR_HISTOGRAM_BUILD_PROGRAMS=OFF
    -DHDR_HISTOGRAM_BUILD_SHARED=OFF
  DEPENDS ${default_depends})

ExternalProject_Add(abseil
  GIT_REPOSITORY https://github.com/abseil/abseil-cpp.git
  GIT_TAG 20220623.0
  INSTALL_DIR    @REDPANDA_DEPS_INSTALL_DIR@
  CMAKE_COMMAND ${CMAKE_COMMAND} -E env ${cmake_build_env} ${CMAKE_COMMAND}
  DEPENDS ${default_depends}
  LIST_SEPARATOR |
  CMAKE_ARGS
    ${common_cmake_args}
    -DABSL_ENABLE_INSTALL=ON)

ExternalProject_Add(crc32c
  GIT_REPOSITORY https://github.com/google/crc32c.git
  GIT_TAG 1.1.2
  INSTALL_DIR    @REDPANDA_DEPS_INSTALL_DIR@
  CMAKE_COMMAND ${CMAKE_COMMAND} -E env ${cmake_build_env} ${CMAKE_COMMAND}
  DEPENDS ${default_depends}
  LIST_SEPARATOR |
  CMAKE_ARGS
    ${common_cmake_args}
    -DCRC32C_BUILD_TESTS=0
    -DCRC32C_BUILD_BENCHMARKS=0
    -DCRC32C_USE_GLOG=0)

ExternalProject_Add(roaring
  GIT_REPOSITORY https://github.com/RoaringBitmap/CRoaring.git
  GIT_TAG v0.6.0
  INSTALL_DIR    @REDPANDA_DEPS_INSTALL_DIR@
  DEPENDS ${default_depends}
  CMAKE_COMMAND ${CMAKE_COMMAND} -E env ${cmake_build_env} ${CMAKE_COMMAND}
  LIST_SEPARATOR |
  CMAKE_ARGS
  ${common_cmake_args}
  -DENABLE_ROARING_TESTS=OFF
  -DROARING_BUILD_STATIC=ON)

ExternalProject_Add(alien_thread
  GIT_REPOSITORY https://github.com/redpanda-data/alien_thread.git
  GIT_TAG bdfb56f
  INSTALL_DIR @REDPANDA_DEPS_INSTALL_DIR@
  DEPENDS ${default_depends}
  PATCH_COMMAND ""
  CONFIGURE_COMMAND ""
  BUILD_COMMAND ""
  INSTALL_COMMAND
  COMMAND ${CMAKE_COMMAND} -E copy_directory <SOURCE_DIR>/include/v <INSTALL_DIR>/include/v)

ExternalProject_Add(base64_ext
  GIT_REPOSITORY https://github.com/aklomp/base64.git
  GIT_TAG v0.5.0
  INSTALL_DIR @REDPANDA_DEPS_INSTALL_DIR@
  DEPENDS ${default_depends}
  CMAKE_COMMAND ${CMAKE_COMMAND} -E env ${cmake_build_env} ${CMAKE_COMMAND}
  LIST_SEPARATOR |
  CMAKE_ARGS
    ${common_cmake_args}
    ${BASE64_CMAKE_ARGS})

ExternalProject_Add(kafka-codegen-pex
  DOWNLOAD_COMMAND ""
  INSTALL_DIR @REDPANDA_DEPS_INSTALL_DIR@
  PATCH_COMMAND ""
  CONFIGURE_COMMAND ""
  BUILD_COMMAND ""
  INSTALL_COMMAND
  COMMAND ${CMAKE_COMMAND} -E chdir <SOURCE_DIR> python3 -m venv env
  COMMAND <SOURCE_DIR>/env/bin/pip install pex==2.1.100
  COMMAND <SOURCE_DIR>/env/bin/pex jsonschema jinja2 -o <INSTALL_DIR>/bin/kafka-codegen-venv)

ExternalProject_Add(avro
  GIT_REPOSITORY https://github.com/redpanda-data/avro
  GIT_TAG 6b2896c2720e799371ea6f35b492d62171872e39
  INSTALL_DIR @REDPANDA_DEPS_INSTALL_DIR@
  CMAKE_COMMAND ${CMAKE_COMMAND} -E env ${cmake_build_env} ${CMAKE_COMMAND}
  LIST_SEPARATOR |
  CMAKE_ARGS
    -DCMAKE_SKIP_INSTALL_ALL_DEPENDENCY=true
    ${common_cmake_args}
  SOURCE_SUBDIR lang/c++
  BUILD_COMMAND ninja avrocpp avrocpp_s avrogencpp
  DEPENDS ${default_depends} boost
)

ExternalProject_Add(rapidjson
  GIT_REPOSITORY https://github.com/redpanda-data/rapidjson.git
  GIT_TAG 27c3a8dc0e2c9218fe94986d249a12b5ed838f1d
  INSTALL_DIR    @REDPANDA_DEPS_INSTALL_DIR@
  CMAKE_COMMAND ${CMAKE_COMMAND} -E env ${cmake_build_env} ${CMAKE_COMMAND}
  DEPENDS ${default_depends}
  LIST_SEPARATOR |
  CMAKE_ARGS
    ${common_cmake_args}
    -DRAPIDJSON_BUILD_EXAMPLES=OFF
    -DRAPIDJSON_BUILD_TESTS=OFF
    -DRAPIDJSON_BUILD_DOC=OFF
    -DRAPIDJSON_HAS_STDSTRING=ON
)

ExternalProject_Add(protobuf
  URL https://github.com/protocolbuffers/protobuf/releases/download/v21.5/protobuf-cpp-3.21.5.tar.gz
  URL_MD5 240e69752162da336c5d1f09c7209fdb
  INSTALL_DIR   @REDPANDA_DEPS_INSTALL_DIR@
  CMAKE_COMMAND ${CMAKE_COMMAND} -E env ${cmake_build_env} ${CMAKE_COMMAND}
  SOURCE_SUBDIR cmake
  LIST_SEPARATOR |
  CMAKE_ARGS
    -Dprotobuf_BUILD_TESTS=OFF
    ${common_cmake_args}
)
